#include "http.hpp"

#define WWWROOT "./wwwroot"
std::string RequestStr(const HttpRequest& req) {
    std::stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    ss << req._path << "\r\n";
    // ss << "==================" << "\r\n";
    for(auto& it : req._params) {
        ss << it.first <<": " << it.second << "\r\n";
    }
    // ss << "------------------------------" << "\r\n";
    for(auto& it : req._headers) {
        ss << it.first << ": " << it.second;
    }
    ss << "\r\n";
    ss << req._body;
    return ss.str();
}
void Hello(const HttpRequest& req,HttpResponse* rsp) {
    rsp->SetContent(RequestStr(req),"text/plain");
    rsp->_statu = 200;

    // sleep(15);
}
void Login(const HttpRequest& req,HttpResponse* rsp) {
    rsp->SetContent(RequestStr(req),"text/plain");
}
void PutFile(const HttpRequest& req,HttpResponse* rsp) {
    std::string pathname = WWWROOT + req._path;
    Util::WriteFile(pathname,req._body);
}
void DelFile(const HttpRequest& req,HttpResponse* rsp) {
    rsp->SetContent(RequestStr(req),"text/plain");
}
int main(int argc, char const *argv[])
{
    HttpServer server(8085);
    server.SetThreadCount(3);
    server.SetBaseDir(WWWROOT); // 设置静态根目录
    server.Get("/hello",Hello);
    server.Post("/login",Login);
    server.Put("/1234.txt",PutFile);
    server.Delete("/12",DelFile);
    server.Listen();
    return 0;
}
